//
//  RCFoodRecommendVC.m
//  RocketProject
//
//  Created by chen jiedan on 13-6-25.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCFoodRecommendVC.h"
#import "IDataModel.h"
#import "RCRecommendFoodDataModel.h"
#import "IManager+FoodRecommend.h"
#import "RCRecommendFoodCell.h"
#import "RCRestaurantRecommendVC.h"

@interface RCFoodRecommendVC ()<IManagerDelegate>{
    BOOL                     isRefresh;
    NSInteger                _pn;
    
}
@property (nonatomic, retain) RCRecommendFoodListDataModel *dataModel;
@property (nonatomic, retain) MBProgressHUDEx *progressHub;
@property (nonatomic, assign) int64_t requestID;
@end

@implementation RCFoodRecommendVC

@synthesize dataModel = _dataModel;
@synthesize progressHub = _progressHub;
@synthesize requestID = _requestID;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void) dealloc
{
    
    self.dataModel = nil;
    self.progressHub.delegate = nil;
    self.progressHub = nil;
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.titleLabelText = @"美食推荐";
    self.navigationItem.titleView = [self createTitleView];
    
    [self refreshData];
    
    // 设置tableview相关参数
    if ( baseVCTableView != nil ) {
        self.view.autoresizingMask |= UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
        baseVCTableView.autoresizingMask |= UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
        baseVCTableView.backgroundColor = [UIColor clearColor];
        baseVCTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        baseVCTableView.frame = self.view.bounds;
        CGRect tableViewNewFrame = CGRectMake(baseVCTableView.frame.origin.x,baseVCTableView.frame.origin.y,baseVCTableView.frame.size.width,UIScreenHeight-20);
        baseVCTableView.frame = tableViewNewFrame;
        baseVCTableView.tableHeaderView = nil;
        baseVCTableView.tableFooterView = nil;
        [self addPullRefresh];
    }
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[IManager sharedInstance]cancelFetchDataRequestByRequestID:_requestID];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark tableview
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataModel.listItems count];
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 109;
}
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RCRecommendFoodCell *cell = [[RCRecommendFoodCell alloc] init];
    [cell bindData:[self.dataModel.listItems objectAtIndex:[indexPath row]]];
    return [cell autorelease];
}

- (void)  tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    RCRecommendFoodItemDataModel *item = [self.dataModel.listItems objectAtIndex:[indexPath row]];
    
    RCRestaurantRecommendVC *vc = [[RCRestaurantRecommendVC alloc] initWithTitle:item.foodName];
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}

-(void) loadMoreData
{
    isRefresh = NO;
    
    NSString *page = [NSString stringWithFormat:@"%d",_pn];
    _requestID = [[IManager sharedInstance] requestRecommendFoodForPage:page withDelegate:self];
    
}



#pragma
#pragma mark ServerData ----下拉刷新数据

-(void) refreshData
{
    _progressHub = [[MBProgressHUDEx alloc] initWithView:self.view
                                               TopHeight:0
                                        withBottomHeight:0
                                           withLeftWidth:0
                                          withRightWidth:0
                                    withExclusiveObjects:nil];
    _progressHub.delegate = self;
    _progressHub.labelText = @"加载中...";
    [self.view addSubview:_progressHub];
    [self.view bringSubviewToFront:_progressHub];
    [_progressHub show:YES];
    
    isRefresh = YES;
    _pn = 0;
    _requestID = [[IManager sharedInstance] requestRecommendFoodForPage:@"0" withDelegate:self];
}



#pragma
#pragma mark IManage Delegate
-(void) updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId
{
    if (_requestID == requestId) {
        
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
        
        if (isRefresh) {
            //下拉刷新
            self.dataModel = (RCRecommendFoodListDataModel *)dataModel;
            [self finishRefreshingData];
        }else{
            //加载更多的
            RCRecommendFoodListDataModel *newData = (RCRecommendFoodListDataModel *)dataModel;
            self.dataModel.pn = newData.pn;
            self.dataModel.rn = newData.rn;
            self.dataModel.total = newData.total;
            [self.dataModel.listItems addObjectsFromArray:newData.listItems];
            
            [baseVCTableView reloadData];
            [self finishLoadMoreData];
        }
        
        
        NSInteger currentTotal = [self.dataModel.listItems count];
        _pn = currentTotal;

        if (currentTotal < self.dataModel.total) {
            [self addPullLoadMore];
        }else{
            [self noMoreData];
        }
    }
    
}

-(void) updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    if (_requestID == requestId) {
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
    }
    
}


@end
